﻿using MediatR;
using RedDragon.Common.Domain.produceplan;
using RedDragon.Common.Infrastructure.Interface;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.produceplanW
{
    public class produceDetailsSearchHandler : IRequestHandler<produceDetailsSearchCommon, Result>
    {
        public readonly IBaseRepository<produceDetails> produceDetailsRepository;

        public produceDetailsSearchHandler(IBaseRepository<produceDetails> produceDetailsRepository)
        {
            this.produceDetailsRepository = produceDetailsRepository;
        }

        public async Task<Result> Handle(produceDetailsSearchCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "主生产计划明细数据显示" };
            try
            {
                var list = from a in produceDetailsRepository.Query()
                           select new ProduceDetailDto
                           {
                               produceDetailsId=a.produceDetailsId,
                               ChooseId = a.ChooseId,
                               ChooseName = a.ChooseId==1?"物料1": a.ChooseId == 2 ? "物料2":"物料3",
                               ChooseAttributeName = a.ChooseAttribute==1?"自制" : a.ChooseAttribute == 2 ? "采购":"委外",
                               Unit = a.Unit,
                               Specification = a.Specification,
                               BomVersionName= a.BomVersion == 1 ? "最新bom版本" : "新版01",
                               PlanCount = a.PlanCount,
                               StartDateTime = a.StartDateTime,
                               EndDateTime = a.EndDateTime,
                               Remark = a.Remark,
                               produceplanNumber=a.produceplanNumber
                           };
                if (request.produceplanNumber != null)
                {
                    list = list.Where(x => x.produceplanNumber == request.produceplanNumber);
                }

                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
    public class ProduceDetailDto
    {
        public int produceDetailsId { get; set; }
        /// <summary>
        /// 物料选择
        /// </summary>
        public int ChooseId { get; set; }
        public string ChooseName { get; set; }
        /// <summary>
        /// 物料属性
        /// </summary>
        public int ChooseAttribute { get; set; }
        public string ChooseAttributeName { get; set; }
        /// <summary>
        /// 单位
        /// </summary>
        public string? Unit { get; set; }
        /// <summary>
        /// 规格
        /// </summary>
        public string? Specification { get; set; }
        /// <summary>
        /// Bom版本
        /// </summary>
        public int BomVersion { get; set; }
        public string BomVersionName { get; set; }
        /// <summary>
        /// 计划数量
        /// </summary>
        public int PlanCount { get; set; }
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartDateTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndDateTime { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark { get; set; }
        /// <summary>
        /// 计划编码
        /// </summary>
        public string? produceplanNumber { get; set; }
    }
}
